package com.wxmp.wxcms.ctrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.wxmp.core.common.BaseCtrl;
import com.wxmp.core.page.Page;
import com.wxmp.core.util.AjaxResult;
import com.wxmp.wxcms.domain.MiniAppCorpBaseInfo;
import com.wxmp.wxcms.domain.MiniAppImages;
import com.wxmp.wxcms.service.MiniAppCorpBaseInfoService;
import com.wxmp.wxcms.service.MiniAppImagesService;

@Controller
@RequestMapping("/miniAppCorpBaseInfo")
public class MiniAppCorpBaseInfoCtrl extends BaseCtrl{
	
	@Autowired
	private MiniAppCorpBaseInfoService  corpBaseInfoService;
	
	@Autowired
	private MiniAppImagesService imagesService;
	
	 @RequestMapping(value = "/list")
	 @ResponseBody
	 public AjaxResult getCorpListByPage(MiniAppCorpBaseInfo entity){
		 List<MiniAppCorpBaseInfo> data= corpBaseInfoService.getCorpListByPage(entity);
		 
		AjaxResult result = new AjaxResult();
		Page newPage = new Page();
		newPage.setPage(entity.getPage());
		newPage.setPageSize(entity.getPageSize());
		newPage.setTotal(entity.getTotal());
		newPage.setTotalPage(entity.getTotalPage());
		result.setPage(newPage);
		result.setData(data);
		result.setSuccess(true);
		return result;
	 }
	 
	 @RequestMapping(value = "/save")
	 @ResponseBody
	 public int add(MiniAppCorpBaseInfo entity) {
		 try
		 {
		 JSONArray imgUrlList=(JSONArray) JSON.parse(entity.getLicenseList());

		//插入到corp_base表里
		corpBaseInfoService.add(entity);
		String corpId= entity.getId();
		
		MiniAppImages obj=new MiniAppImages();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		
		obj.setCorpid(corpId);
		obj.setAddtime(df.format(new Date()));
		
		for(int i=0;i<imgUrlList.size();i++){ 
			obj.setPicurl("");
			 // 遍历 jsonarray 数组，把每一个对象转成 json 对象 
			System.out.println("打印"+i+"----"+imgUrlList.get(i));
			Object imgurl = imgUrlList.get(i);   
			obj.setPicurl(imgurl.toString());
			//插入到image 表里
			imagesService.add(obj);
		} 
			return 1;
		}
		catch(Exception ex) {
			return 0;
		}
	 }
	 
	 
	@RequestMapping(value = "/deleteById")
	@ResponseBody
	public int deleteById(String id) {
	 	return corpBaseInfoService.deleteById(id);
	}
}
